package com.ruoyi.loan.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.TbLoanProductsIdVo;
import com.ruoyi.common.core.domain.TbLoanProductsVo;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.loan.product.domain.TbCategories;
import com.ruoyi.loan.product.domain.TbLoanProducts;
import com.ruoyi.loan.product.mapper.TbCategoriesMapper;
import com.ruoyi.loan.product.mapper.TbLoanProductsMapper;
import com.ruoyi.loan.product.service.TbLoanProductsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class TbLoanProductsServiceImpl extends ServiceImpl<TbLoanProductsMapper, TbLoanProducts>
    implements TbLoanProductsService{

    @Resource
    TbLoanProductsMapper tbLoanProductsMapper;

    @Resource
    TbCategoriesMapper tbCategoriesMapper;

    @Override
    public R add(TbLoanProducts products) {
        try {
           tbLoanProductsMapper.insert(products);
            return R.ok(products,"添加成功");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return R.fail("添加失败");
    }

    @Override
    public R updateByProduct(TbLoanProducts products) {
        try {
            tbLoanProductsMapper.updateById(products);
            return R.ok(products,"修改成功");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return R.fail("修改失败");
    }

    @Override
    public R deleteById(TbLoanProductsIdVo productsIdVo) {

        try {
            tbLoanProductsMapper.deleteById(productsIdVo.getLoanProductsId());
            return R.ok(productsIdVo,"删除成功");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return R.fail("删除失败");
    }

    @Override
    public List<TbLoanProductsVo> findAll(TbLoanProductsVo productsVo) {
        List<TbLoanProductsVo> listVo = new ArrayList<>();
        QueryWrapper<TbLoanProducts> queryWrapper = new QueryWrapper<>();
        if(productsVo.getLoanProductsName()!=null){
            queryWrapper.lambda().eq(TbLoanProducts::getLoanProductsName,productsVo.getLoanProductsName());
        }
        List<TbLoanProducts> list = tbLoanProductsMapper.selectList(queryWrapper);
        for(int i=0;i<list.size();i++){
            TbLoanProductsVo tbLoanProductsVo = new TbLoanProductsVo();
            TbCategories tbCategories = tbCategoriesMapper.selectById(list.get(i).getLoanProductsCategoriesId());
            tbLoanProductsVo.setLoanProductsCategoriesName(tbCategories.getCategoriesName());
            BeanUtils.copyProperties(list.get(i),tbLoanProductsVo);
            listVo.add(tbLoanProductsVo);
        }
        return listVo;
    }
}




